package com.gimi.cloud.bbp.client.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.gimi.cloud.bbp.client.service.home.HomePageService;
import com.gimi.cloud.bbp.common.constant.RedisKeyConstant;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.commonService.redis.RedisService;
import com.gimi.cloud.bbp.dto.base.BaseDTO;
import com.gimi.cloud.bbp.dto.mall.MallAppSpecialDTO;
import com.gimi.cloud.bbp.dto.mall.MallAppSpecialGroupDTO;

/**
 * Created by shazhiwei on 2017/12/27.
 */
@RestController
@RequestMapping("/homePage")
public class HomePageController {
    private final HomePageService homePageService;
    private final RedisService redisService;
    private static Logger logger = LoggerFactory.getLogger(HomePageController.class);

    public HomePageController(HomePageService homePageService, RedisService redisService){
        this.homePageService = homePageService;
        this.redisService = redisService;
    }

    @RequestMapping(value = "/columnList")
    public void columnList(@RequestBody BaseDTO params, HttpServletResponse response) {
        long start = System.currentTimeMillis();
        Util.sendResponse(homePageService.homePageColumnList(), response);
        logger.info(String.format("===>耗时%d",System.currentTimeMillis() - start));
    }

    @RequestMapping(value = "/majorAdList")
    public void majorAdList(@RequestBody BaseDTO params, HttpServletResponse response) {
        long start = System.currentTimeMillis();
        Util.sendResponse(homePageService.homePageMajorAdList(), response);
        logger.info(String.format("===>耗时%d",System.currentTimeMillis() - start));
    }
    
    /**
     * 获取首页轮播图
     * @param params
     * @param response
     */
    @RequestMapping(value = "/getHomePageCarouselFigure")
    public void getHomePageCarouselFigure(@RequestBody BaseDTO params, HttpServletResponse response) {
    	Util.sendResponse(JSON.toJSONString(homePageService.getHomePageCarouselFigure(params)), response);
    }

    /**
     * @param mallCart
     * @param response
     */
    @RequestMapping(value = "/getHomePageData")
	public void getHomePageData(@RequestBody BaseDTO params, HttpServletResponse response) {
		Util.sendResponse(JSON.toJSONString(homePageService.getHomePageData(params)),response);
	}
    
    /**
     * @param mallCart
     * @param response
     */
    @RequestMapping(value = "/getIndex")
	public void getIndex(@RequestBody MallAppSpecialGroupDTO params, HttpServletResponse response) {
    	String redisKey = RedisKeyConstant.APP_PAGE_CACHE + "index";
		String pageCache = redisService.get(redisKey);
		if (!StringUtil.isEmpty(pageCache)) {
			Util.sendResponse(pageCache, response);
			logger.info("调用页面缓存返回");
			return;
		}
    	Map<String, Object> map = new HashMap<>();
    	map.put("banner", homePageService.getBanner(null));
    	map.put("recomment", homePageService.getRecomment(null));
    	map.put("special", homePageService.getSpecial(params));
    	
    	Response resp = new Response();
        resp.setRes_data(map);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        String resultStr = JsonUtil.toJson(resp);
    	redisService.set(redisKey, resultStr, 2L, TimeUnit.MINUTES);
        logger.info("页面缓存设置成功key：" + redisKey);
        Util.sendResponse(resultStr, response);
	}
    
    @RequestMapping(value = "/getSpecialList")
    public void getSpecialList(@RequestBody MallAppSpecialDTO params, HttpServletResponse response) {
    	String redisKey = RedisKeyConstant.APP_PAGE_CACHE + "getSpecialList:" + params.getAsgId() + "pageNums-" + params.getPageNums() + "pageSize-" + params.getPageSizes();
		String pageCache = redisService.get(redisKey);
		if (!StringUtil.isEmpty(pageCache)) {
			Util.sendResponse(pageCache, response);
			logger.info("调用页面缓存返回");
			return;
		}
    	Map<String, Object> map = new HashMap<>();
    	map.put("special", homePageService.getSpecialListByAsgId(params));
    	
    	Response resp = new Response();
        resp.setRes_data(map);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        String resultStr = JsonUtil.toJson(resp);
    	redisService.set(redisKey, resultStr, 2L, TimeUnit.MINUTES);
        logger.info("页面缓存设置成功key：" + redisKey);
        Util.sendResponse(resultStr, response);
    	
    }
    
}
